package com.lanou.bookstore.book.web.servlet.admin;

import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.book.service.BookService;
import com.lanou.bookstore.category.domain.Category;
import com.lanou.bookstore.category.service.CategoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/adminBook")
public class AdminBookServlet extends HttpServlet {
    private BookService bookService = new BookService();
    private CategoryService categoryService = new CategoryService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if ("删除".equals(method)) {
            Integer bid = Integer.parseInt(request.getParameter("bid"));
            bookService.updateDelByBid(bid);
            List<Book> bookList = bookService.findAll();
            request.setAttribute("bookList", bookList);
            request.getRequestDispatcher("/adminjsps/admin/book/list.jsp").forward(request, response);
        } else if ("修改".equals(method)) {
            Integer bid = Integer.parseInt(request.getParameter("bid"));
            String bname = request.getParameter("bname");
            Double price = Double.parseDouble(request.getParameter("price"));
            String author = request.getParameter("author");
            String image = request.getParameter("image");
            String cid = request.getParameter("cid");

            Book book = new Book(bid, bname, price, author, image, cid, 0);
            bookService.updateAll(book);
            request.setAttribute("book", book);
            request.getRequestDispatcher("/adminjsps/admin/book/desc.jsp").forward(request, response);
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bid = request.getParameter("bid");
        String cid = request.getParameter("cid");

        if (bid == null && cid == null) {
            List<Book> bookList = bookService.findAll();
            request.setAttribute("bookList", bookList);
            request.getRequestDispatcher("/adminjsps/admin/book/list.jsp").forward(request, response);
        } else if (bid != null && cid == null) {
            Book book = bookService.findByBid(bid);
            List<Category> categoryList = categoryService.findAll();
            request.setAttribute("book", book);
            request.setAttribute("categoryList", categoryList);
            request.getRequestDispatcher("/adminjsps/admin/book/desc.jsp").forward(request, response);
        } else if (bid == null && cid != null) {
            List<Category> categoryList = categoryService.findAll();
            request.setAttribute("categoryList", categoryList);
            request.getRequestDispatcher("/adminjsps/admin/book/add.jsp").forward(request, response);
        }

    }
}
